﻿using System;
using System.Web.Configuration;
using System.Web.UI;

namespace VioletDevelopmentLaboratory.Net.Web.Configuration
{
	public sealed class PagesSectionManager : ConfigurationManagerBase
	{
		private PagesSection section;

		public PagesSectionManager()
		{
			this.section = (PagesSection)base.GetConfigurationSection("system.web/pages");
		}

		public void Modify(
			TimeSpan asyncTimeout,
			bool autoEventWireup,
			bool buffer,
			CompilationMode compilationMode,
			PagesEnableSessionState enableSessionState,
			bool enableEventValidation,
			bool enableViewState,
			bool enableViewStateMac,
			bool maintainScrollPositionOnPostBack,
			string masterPageFile,
			int maxPageStateFieldLength,
			string pageBaseType,
			string pageParserFilterType,
			string styleSheetTheme,
			string theme,
			string userControlBaseType,
			bool validateRequest,
			ViewStateEncryptionMode viewStateEncryptionMode)
		{
			this.section.AsyncTimeout = asyncTimeout;
			this.section.AutoEventWireup = autoEventWireup;
			this.section.Buffer = buffer;
			this.section.CompilationMode = compilationMode;
			this.section.EnableEventValidation = enableEventValidation;
			this.section.EnableSessionState = enableSessionState;
			this.section.EnableViewState = enableViewState;
			this.section.EnableViewStateMac = enableViewStateMac;
			this.section.MaintainScrollPositionOnPostBack = maintainScrollPositionOnPostBack;
			this.section.MasterPageFile = masterPageFile;
			this.section.MaxPageStateFieldLength = maxPageStateFieldLength;
			if (string.IsNullOrEmpty(pageBaseType))
			{
				pageBaseType = "System.Web.UI.Page";
			}
			this.section.PageBaseType = pageBaseType;
			this.section.PageParserFilterType = pageParserFilterType;
			this.section.StyleSheetTheme = styleSheetTheme;
			this.section.Theme = theme;
			if (string.IsNullOrEmpty(userControlBaseType))
			{
				userControlBaseType = "System.Web.UI.UserControl";
			}
			this.section.UserControlBaseType = userControlBaseType;
			this.section.ValidateRequest = validateRequest;
			this.section.ViewStateEncryptionMode = viewStateEncryptionMode;
			this.Save();
		}
		public PagesSection GetPages()
		{
			return this.section;
		}





		public void AddControl(string tagPrefix, string name_space, string assembly, string tagName, string source)
		{
			TagPrefixInfo tag = new TagPrefixInfo(tagPrefix, name_space, assembly, tagName, source);
			this.section.Controls.Add(tag);
			this.Save();
		}
		public void ModifyControl(int index, string tagPrefix, string name_space, string assembly, string tagName, string source)
		{
			TagPrefixInfo tag = this.section.Controls[index];

			tag.TagPrefix = tagPrefix;
			tag.Namespace = name_space;
			tag.Assembly = assembly;
			tag.TagName = tagName;
			tag.Source = source;

			this.Save();
		}
		public void RemoveControl(int index)
		{
			this.section.Controls.Remove(GetControl(index));
			this.Save();
		}
		public void ClearControls()
		{
			this.section.Controls.Clear();

			this.Save();
		}
		public TagPrefixInfo GetControl(int index)
		{
			return this.section.Controls[index];
		}
		public TagPrefixCollection GetControls()
		{
			return this.section.Controls;
		}




		public void AddNamespace(string name_space)
		{
			this.section.Namespaces.Add(new NamespaceInfo(name_space));
			this.Save();
		}
		public void ModifyNamespace(int index, string name_space)
		{
			this.section.Namespaces[index].Namespace = name_space;
			this.Save();
		}
		public void RemoveNamespace(int index)
		{
			this.section.Namespaces.RemoveAt(index);
			this.Save();
		}
		public void ClearNamespace()
		{
			this.section.Namespaces.Clear();
			this.Save();
		}
		public NamespaceInfo GetNamespace(int index)
		{
			return this.section.Namespaces[index];
		}
		public NamespaceCollection GetNamespaces()
		{
			return this.section.Namespaces;
		}





		public void AddTagMap(string tagType, string mappedTagType)
		{
			TagMapInfo tagMap = new TagMapInfo(tagType, mappedTagType);
			this.section.TagMapping.Add(tagMap);
			this.Save();
		}
		public void ModifyTagMap(int index, string tagType, string mappedTagType)
		{
			TagMapInfo tagMap = this.section.TagMapping[index];

			tagMap.TagType = tagType;
			tagMap.MappedTagType = mappedTagType;

			this.Save();
		}
		public void RemoveTagMap(int index)
		{
			TagMapInfo tagMap = GetTagMap(index);

			this.section.TagMapping.Remove(tagMap);
			this.Save();
		}
		public void ClearTagMaps()
		{
			this.section.TagMapping.Clear();

			this.Save();
		}
		public TagMapInfo GetTagMap(int index)
		{
			return this.section.TagMapping[index];
		}
		public TagMapCollection GetTagMaps()
		{
			return this.section.TagMapping;
		}
	}
}